<?php

namespace app\admin\common\business\admin;

use app\BaseController;
use app\admin\model\admin\AdminUser as AdminUserModel;

class AdminUser extends BaseController
{
    /**
     * @param $data
     * @return \think\response\Json
     */
    public function passwordAddSalt($data)
    {
        if (empty($data['password'])) {
            return $this -> show(
                config("status.error"),
                config("status.key_error"),
                NULL
            );
        }
        $salt = $this -> salt();
        $ip = request() -> ip();
        $data["password"] = md5($salt . $data['password'] . $salt);
        $data["password_salt"] = $salt;
        $data["last_login_ip"] = $ip;
        $data['last_login_time'] = time();
        return $data;
    }

    public function add($data): bool
    {
        $execute = new AdminUserModel();
        return $execute->addAdminUser($data);
    }

    /**
     * @return false|string
     */
    public function salt()
    {
        //盐字符集
        $str = "1234567890abcdefghijklmnopqrstuvwxyz";
        $str = str_shuffle($str); // 打乱字符串
        $str = substr($str, 0, 4); // 截取字符串
        return $str;
    }
}